/*
 * @Description: 档案鉴定管理列表
 * @Author: Rfan
 * @Date: 2022-07-29 16:18:02
 * @LastEditTime: 2022-07-29 19:22:29
 */

import { useEffect, useMemo, useState } from 'react';
import { Button, Space, Table } from 'antd';
import classNames from 'classnames/bind';
import { useFormatMessage } from '@/utils/intlHelpers';
import { formatDate } from '@/utils';
import { useAppraisalMgtContext } from '../contexts/AppraisalMgtContext';
import AppraisalDialog from './dialogs/DoAppraisalDialog';
import EditBatchDialog from './dialogs/EditBatchDialog';
import type { IPageQuery } from '@/interfaces/global';
import type { IDispositionPendingBatchParams } from '@/api/archives/disposition';
import styles from '../styles/pageList.module.scss';
import { IBatchItemResponse } from '@/api/archives/disposition';

const cx = classNames.bind(styles);
const { Column } = Table;

const PageList = (props: any) => {
  const { className } = props;
  const { selectATId, batchList, selectItems, setSelectKey, getBatchListFunc, deleteBatchFunc } =
    useAppraisalMgtContext();
  const formatMessage = useFormatMessage();

  const [loading, setLoading] = useState(false);
  const [total, setTotal] = useState(0);
  const [listQuery, setListQuery] = useState<IPageQuery<Partial<IDispositionPendingBatchParams>>>({
    pageNo: 1,
    pageSize: 10,
    param: {
      archiveTypeId: 65
    }
  });
  // 处理条目信息
  const [editInfo, setEditInfo] = useState<IBatchItemResponse>();
  // 编辑弹窗展示状态
  const [editBatchDialogVisible, setEditBatchDialogVisible] = useState(false);
  // 批次详情弹窗展示状态
  const [appraisalDialogVisible, setAppraisalDialogVisible] = useState(false);

  useEffect(() => {
    if (selectATId) {
      setListQuery({
        pageNo: 1,
        pageSize: 10,
        param: {
          archiveTypeId: selectATId
        }
      });
    }
  }, [selectATId]);

  useEffect(() => {
    if (listQuery && listQuery.param?.archiveTypeId) {
      setLoading(true);
      getBatchListFunc(listQuery)
        .then((res: any) => {
          setTotal(res?.totalCount);
        })
        .finally(() => {
          setLoading(false);
        });
    }
  }, [listQuery]);

  /**
   * @description 切换页码触发
   * @param {number} page 页码
   * @param {number} pageSize 每页条目数
   */
  const pageChange = (page: number, pageSize: number) => {
    setListQuery((state) => ({
      ...state,
      pageNo: page,
      pageSize
    }));
  };

  /**
   * @description : 表格选项相关设置
   * @param {number} selectedRowKeys 选择的项目的KEY
   */
  const onSelectChange = (selectedRowKeys: number[]) => {
    setSelectKey(selectedRowKeys);
  };

  /**
   * @description:  表格多选配置
   */
  const rowSelection = {
    selectItems,
    onChange: onSelectChange
  };

  const showEditDialog = (data: IBatchItemResponse) => {
    setEditInfo(data);
    setEditBatchDialogVisible(true);
  };

  const showAppraisalDialog = (data: IBatchItemResponse) => {
    setEditInfo(data);
    setAppraisalDialogVisible(true);
  };

  return (
    <div className={className}>
      <Table
        dataSource={batchList}
        loading={loading}
        rowKey="id"
        scroll={{ x: 'max-content' }}
        bordered
        rowSelection={rowSelection}
        pagination={{
          current: listQuery.pageNo,
          pageSize: listQuery.pageSize,
          total,
          onChange: pageChange
        }}
      >
        <Column title="序号" dataIndex="id" key="id" align="center" />
        <Column title="鉴定批次标题" dataIndex="batchName" key="batchName" />
        <Column title="全宗" dataIndex="fondName" key="fondName" />
        <Column title="档案类型" dataIndex="atName" key="atName" />
        <Column title="鉴定类型" dataIndex="name" key="name" />
        <Column title="鉴定目的" dataIndex="remark" key="remark" />
        <Column title="创建人" dataIndex="operatorName" key="operatorName" />
        <Column
          title="创建时间"
          dataIndex="createTime"
          key="createTime"
          render={(text) => (text ? formatDate(text) : '')}
        />
        <Column
          title="完成时间"
          dataIndex="finishTime"
          key="finishTime"
          align="center"
          render={(text) => (text ? formatDate(text) : '')}
        />
        <Column
          title={formatMessage('tableAction')}
          key="action"
          className={cx('action-col')}
          align="center"
          render={(_text, record: any) => (
            <Space>
              <Button
                type="link"
                size="small"
                onClick={() => {
                  showEditDialog(record);
                }}
              >
                修改
              </Button>
              <Button
                type="link"
                size="small"
                onClick={() => {
                  showAppraisalDialog(record);
                }}
              >
                处理
              </Button>
              <Button type="link" size="small" disabled>
                查看
              </Button>
              <Button
                type="link"
                size="small"
                danger
                onClick={() => {
                  deleteBatchFunc(record.id);
                }}
              >
                删除
              </Button>
            </Space>
          )}
        />
      </Table>
      {useMemo(
        () => (
          <EditBatchDialog
            visible={editBatchDialogVisible}
            data={editInfo}
            onClose={() => {
              setEditBatchDialogVisible(false);
            }}
          />
        ),
        [editBatchDialogVisible]
      )}
      {useMemo(
        () => (
          <>
            {editInfo && (
              <AppraisalDialog
                visible={appraisalDialogVisible}
                data={editInfo}
                onClose={() => {
                  setAppraisalDialogVisible(false);
                }}
              />
            )}
          </>
        ),
        [appraisalDialogVisible]
      )}
    </div>
  );
};

export default PageList;
